import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'auth_provider.dart';

class AuthListener extends ConsumerWidget {
  final Widget child;
  
  const AuthListener({Key? key, required this.child}) : super(key: key);

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    ref.listen<AuthState>(authStateProvider, (previous, next) {
      // 监听登录状态变化
      if (previous?.isLoggedIn != next.isLoggedIn) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
            content: Text(next.isLoggedIn ? '登录成功' : '已退出登录'),
            backgroundColor: next.isLoggedIn ? Colors.green : Colors.orange,
          ),
        );
      }
      
      // 监听错误
      if (next.error != null && previous?.error != next.error) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
            content: Text(next.error!),
            backgroundColor: Colors.red,
          ),
        );
      }
    });
    
    return child;
  }
}